<?php
declare (strict_types = 1);

namespace app\wechat\controller;

use app\BaseController;
use app\Request;
use app\wechat\model\OfficeMessageModel;
use app\wechat\model\OfficeQrcodeModel;
use app\wechat\model\OfficeTemplateModel;
use app\wechat\model\OfficeTemplateSendRecordModel;
use app\wechat\model\OfficeUserModel;
use app\wechat\service\OfficeService;
use think\facade\View;

class Office extends BaseController
{
    /**
     * 发送模板消息
     *
     * @Author tanruizheng
     * @DateTime 2021-08-20
     * @return void
     */
    public function sendTemplateMessage(Request $request){
        try {
            $openId = 'okYDX50ByRlPu-H0XQ73cZk3Awf8';
            $templateId = 'QPWuQXFbGMafKsYWxELQ_OglNzJcMA15_h1Ir0aiAjM';
            $url = '';
            $miniprogram = [];
            $data = [];
            $officeService = new OfficeService('wx22650a4626757a41');
            $res = $officeService->sendTemplateMessage($openId, $templateId,$data, $url, $miniprogram);
            if($res == false){
                return json(self::createFail($officeService->getError()));
            }
            return json(self::createSuccess('发送模板成功'));
        } catch (\Exception $e) {
            return json(self::createFail($e->getMessage()));
        }
    }

    /**
     * 创建临时二维码
     *
     * @Author tanruizheng
     * @DateTime 2021-08-20
     * @return void
     */
    public function createTemporaryQrcode(){
        try {
            $officeService = new OfficeService('wx22650a4626757a41');
            $res = $officeService->createTemporaryQrcode('foo',6 * 24 * 3600);
            return json($res);
        } catch (\Exception $e) {
            return json(self::createFail($e->getMessage()));
        }
    }

    /**
     * 创建永久二维码
     *
     * @Author tanruizheng
     * @DateTime 2021-08-20
     * @return void
     */
    public function createForverQrcode(){
        try {
            $officeService = new OfficeService('wx22650a4626757a41');
            $res = $officeService->createForverQrcode('foo');
            return json($res);
        } catch (\Exception $e) {
            return json(self::createFail($e->getMessage()));
        }
    }

    /**
     * 自动回复
     *
     * @Author tanruizheng
     * @DateTime 2021-08-23
     * @return void
     */
    public function push(){
        try {
            $officeService = new OfficeService('wx22650a4626757a41');
            $officeService->getApp()->server->push(function ($message) use($officeService) {
                //记录接收消息
                $officeService->handleMessage($message);
                switch ($message['MsgType']) {
                    case 'event':
                        return '收到事件消息';
                        break;
                    case 'text':
                        return '收到文字消息';
                        break;
                    case 'image':
                        return '收到图片消息';
                        break;
                    case 'voice':
                        return '收到语音消息';
                        break;
                    case 'video':
                        return '收到视频消息';
                        break;
                    case 'location':
                        return '收到坐标消息';
                        break;
                    case 'link':
                        return '收到链接消息';
                        break;
                    case 'file':
                        return '收到文件消息';
                    // ... 其它消息
                    default:
                        return '收到其它消息';
                        break;
                }
            });
            
            $response = $officeService->getApp()->server->serve();
            
            // $response 为 `Symfony\Component\HttpFoundation\Response` 实例
            // 对于需要直接输出响应的框架，或者原生 PHP 环境下
            $response->send();
        } catch (\Exception $e) {
            return json(self::createFail($e->getMessage()));
        }
    }

   /**
     * 微信公众号授权用户
     *
     * @Author tanruizheng
     * @DateTime 2021-08-25
     * @return void
     */
    public function user(Request $request){
        if($request->param('_action') == 'getList'){
            $page = $request->param('page/d',1);
            $limit = $request->param('limit/d',15);
            $where = [];
            //列表
            $list = OfficeUserModel::where($where)->page($page,$limit)->select()->toArray();
            foreach ($list as &$item) {
              
            }
            //总数
            $count = OfficeUserModel::where($where)->count();
            return json(self::createList($list,$count));
        }else if($request->param('_action') == 'delete'){
            $id = $request->param('id');
            $OfficeUserModel = OfficeUserModel::where('id',$id)->findOrEmpty();
            if($OfficeUserModel->isEmpty()){
                return self::createFail('信息不存在');
            }
            $res = $OfficeUserModel->delete();
            if($res !== false){
                return self::createSuccess('删除成功');
            }
            return self::createFail('删除失败');
        }
        return View::fetch('user',[
           
        ]);
    }

    /**
     * 消息模板
     *
     * @Author tanruizheng
     * @DateTime 2021-08-25
     * @return void
     */
    public function templateList(Request $request){
        if($request->param('_action') == 'getList'){
            $page = $request->param('page/d',1);
            $limit = $request->param('limit/d',15);
            $where = [];
            //列表
            $list = OfficeTemplateModel::where($where)->page($page,$limit)->select()->toArray();
            foreach ($list as &$item) {
              
            }
            //总数
            $count = OfficeTemplateModel::where($where)->count();
            return json(self::createList($list,$count));
        }else if($request->param('_action') == 'delete'){
            $id = $request->param('id');
            $OfficeTemplateModel = OfficeTemplateModel::where('id',$id)->findOrEmpty();
            if($OfficeTemplateModel->isEmpty()){
                return self::createFail('信息不存在');
            }
            $res = $OfficeTemplateModel->delete();
            if($res !== false){
                return self::createSuccess('删除成功');
            }
            return self::createFail('删除失败');
        }else if($request->param('_action') == 'getPrivateTemplates'){
            $officeService = new OfficeService('wx22650a4626757a41');
            $res = $officeService->getPrivateTemplates();
            return json($res);
        }
        return View::fetch('templateList',[
           
        ]);
    }

    /**
     * 参数二维码
     *
     * @Author tanruizheng
     * @DateTime 2021-08-25
     * @return void
     */
    public function qrcode(Request $request){
        if($request->param('_action') == 'getList'){
            $page = $request->param('page/d',1);
            $limit = $request->param('limit/d',15);
            $where = [];
            //列表
            $list = OfficeQrcodeModel::where($where)->page($page,$limit)->select()->toArray();
            foreach ($list as &$item) {
               
            }
            //总数
            $count = OfficeQrcodeModel::where($where)->count();
            return json(self::createList($list,$count));
        }else if($request->param('_action') == 'miniCodeDel'){
            $id = $request->param('id');
            $OfficeQrcodeModel = OfficeQrcodeModel::where('id',$id)->findOrEmpty();
            if($OfficeQrcodeModel->isEmpty()){
                return self::createFail('信息不存在');
            }
            $res = $OfficeQrcodeModel->delete();
            if($res !== false){
                return self::createSuccess('删除成功');
            }
            return self::createFail('删除失败');
        }else if($request->param('_action') == 'qrcodeCreate'){
            return View::fetch('qrcodeCreate');
        }else if($request->param('_action') == 'qrcodeCreatePost'){
            try {
                $param = $request->param('param');
                $officeService = new OfficeService('wx22650a4626757a41');
                $res = $officeService->createTemporaryQrcode($param,6 * 24 * 3600);
                return json($res);
            } catch (\Exception $e) {
                return json(self::createFail($e->getMessage()));
            }
        }
        return View::fetch('qrcode',[
           
        ]);
    }

     /**
     * 自动回复
     *
     * @Author tanruizheng
     * @DateTime 2021-08-25
     * @return void
     */
    public function message(Request $request){
        if($request->param('_action') == 'getList'){
            $page = $request->param('page/d',1);
            $limit = $request->param('limit/d',15);
            $where = [];
            //列表
            $list = OfficeMessageModel::where($where)->page($page,$limit)->select()->toArray();
            foreach ($list as &$item) {
              
            }
            //总数
            $count = OfficeMessageModel::where($where)->count();
            return json(self::createList($list,$count));
        }else if($request->param('_action') == 'delete'){
            $id = $request->param('id');
            $OfficeMessageModel = OfficeMessageModel::where('id',$id)->findOrEmpty();
            if($OfficeMessageModel->isEmpty()){
                return self::createFail('信息不存在');
            }
            $res = $OfficeMessageModel->delete();
            if($res !== false){
                return self::createSuccess('删除成功');
            }
            return self::createFail('删除失败');
        }
        return View::fetch('message',[
           
        ]);
    }
}
